package com.cloudkinto.controller.order;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.order.OrderAmazonService;
import com.cloudkinto.service.order.vo.Export2ShipOrderReq;
import com.cloudkinto.service.order.vo.OrderAmazonExportReq;
import com.cloudkinto.service.order.vo.amazon.AmazonOrderDetailRes;
import com.cloudkinto.service.order.vo.amazon.AmazonOrderDownReq;
import com.cloudkinto.service.order.vo.amazon.UpdateSysMemoReq;
import com.cloudkinto.service.orderoutput.vo.OrderOutputAddReq;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-06-02
 */
@RestController
@Api(tags = "亚马逊订单模块")
@RequestMapping("/api/admin/orderAmazon")
public class OrderAmazonController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(OrderAmazonController.class);

    @Autowired
    private OrderAmazonService service;

    @UserSessionRequired
    @GetMapping("/downloadInit")
    @ApiOperation(value = "下载亚马逊订单初始化", notes = "初始化")
    public SingleResult downloadInit() {
        return service.addInit(getCurrentUserId(), getCompanyId());
    }

    @GetMapping("/adToken")
    @ApiOperation(value = "下载亚马逊订单初始化", notes = "初始化")
    public SingleResult adToken(String code) {
        return service.adToken(code);
    }

    @UserSessionRequired
    @PostMapping("downloadAmazonOrders")
    @ApiOperation(value = "下载亚马逊订单", notes = "下载亚马逊订单")
    public SingleResult downloadAmazonOrders(@RequestBody @Validated AmazonOrderDownReq req) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.selectAmazonOrderApi(req, userId, companyId);
    }

    @UserSessionRequired
    @PostMapping("exportExcel")
    @ApiOperation(value = "导出所有数据excel")
    public void exportAll(@RequestBody OrderAmazonExportReq request, HttpServletResponse response) throws Exception {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        map.put("keyword", request.keyword);
        map.put("startTime", request.startTime);
        map.put("endTime", request.endTime);
        map.put("orderStatus", request.orderStatus);
        map.put("fulfillmentChannel", request.fulfillmentChannel);
        map.put("isSendOut", request.isSendOut);
        service.exportExcel(map, request.ids, response);
    }

    @UserSessionRequired
    @PostMapping("mark/sendOut")
    @ApiOperation(value = "标记为发货", notes = "标记为发货")
    public SingleResult markSendOut(@RequestBody List<Long> ids) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.markSendOut(ids, userId);
    }

    @UserSessionRequired
    @PutMapping("systemMemo")
    @ApiOperation(value = "编辑系统备注", notes = "")
    public SingleResult systemMemo(@RequestBody @Validated UpdateSysMemoReq req) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return service.updateSysMemo(req, userId, companyId);
    }

//    @UserSessionRequired
//    @GetMapping("systemMemoInit")
//    @ApiOperation(value = "编辑系统备注初始化", notes = "")
//    public SingleResult systemMemoInit(@RequestParam Long id) throws Exception {
//        return service.systemMemoInit(id);
//    }

    @UserSessionRequired
    @PostMapping("sendOut")
    @ApiOperation(value = "发货", notes = "发货")
    public SingleResult sendOut(@RequestBody Export2ShipOrderReq req) throws Exception {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        if (req.getIsSolo() == 0) {
            return service.sendOut(req.getOrderids(), userId, companyId, false);
        } else {
            return service.sendOutNopackage(req.getOrderids()[0], userId, companyId);
        }
    }

    @UserSessionRequired
    @PostMapping("sendOutMatch")
    @ApiOperation(value = "确认发货发货", notes = "确认发货发货")
    public SingleResult sendOutMatch(@RequestBody List<OrderOutputAddReq> orderOutputAddReqList) throws Exception {
        Long userId = getCurrentUserId();
        return service.sendOutMatch(orderOutputAddReqList, userId);
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AmazonOrderDetailRes.class)})
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态"),
            @ApiImplicitParam(name = "shopId", value = "店铺id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "fulfillmentChannel", value = "转运"),
            @ApiImplicitParam(name = "isSend", value = "是否配送"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")})
    public PageResult listPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String shopId = request.getParameter("shopId");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String orderStatus = request.getParameter("orderStatus");
        String fulfillmentChannel = request.getParameter("fulfillmentChannel");
        String isSendOut = request.getParameter("isSend");

        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");

        if (companyId != null) {
            map.put("companyId", companyId + "");
        }
        map.put("keyword", keyword);
        map.put("startTime", startTime);
        map.put("shopId", shopId);
        map.put("endTime", endTime);
        map.put("orderStatus", orderStatus);
        map.put("fulfillmentChannel", fulfillmentChannel);
        map.put("isSendOut", isSendOut);
        map.put("current", current);
        map.put("pageSize", size);
        return service.getListPage(map);
    }

}

